Skip to content

Reject negative ln inputs#22276

Open
akmhatey-ai wants to merge 3 commits into
apache:mainfrom
akmhatey-ai:codex/ln-negative-input-error
Open

Reject negative ln inputs#22276
akmhatey-ai wants to merge 3 commits into
apache:mainfrom
akmhatey-ai:codex/ln-negative-input-error

Conversation

@akmhatey-ai

Copy link
Copy Markdown

Which issue does this PR close?

Rationale for this change

DataFusion currently returns NaN for negative ln inputs. PostgreSQL raises an error for the same case, and issue #22271 tracks aligning ln((-1.0)::float8) with that behavior.

This keeps the existing ln(0) behavior unchanged.

What changes are included in this PR?

  • Move ln from the shared unary math macro to a dedicated LnFunc implementation.
  • Return a compute error when ln receives a negative Float32 or Float64 value.
  • Update scalar.slt to cover positive column inputs plus negative scalar and column error cases.

Are these changes tested?

Yes. I ran:

  • cargo fmt --check -- datafusion/functions/src/math/mod.rs datafusion/functions/src/math/ln.rs
  • cargo check -p datafusion-functions
  • cargo test -p datafusion-functions math::ln --lib
  • cargo test -p datafusion-sqllogictest --test sqllogictests -- scalar:586
  • cargo test -p datafusion-sqllogictest --test sqllogictests -- scalar:593
  • cargo test -p datafusion-sqllogictest --test sqllogictests -- scalar:597
  • git diff --check

I also ran cargo test -p datafusion-sqllogictest --test sqllogictests -- scalar. The changed ln records passed, but the full file failed later at scalar.slt:1546+ because local aggregate_test_100 data was empty in this checkout; those failures were unrelated to this change.

Are there any user-facing changes?

Yes. ln now errors on negative inputs instead of returning NaN.

@github-actions github-actions Bot added sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation labels May 16, 2026

@kumarUjjawal kumarUjjawal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also need to look into other such log functions which also return NaN.

DataFusion CLI v53.1.0
> >   SELECT
    ln(2.0)    AS ln_pos,
    log10(100.0) AS log10_pos,
    log(-1.0)    AS log_neg,
    log2(-1.0)   AS log2_neg,
    log10(-1.0)  AS log10_neg;
+--------------------+-----------+---------+----------+-----------+
| ln_pos             | log10_pos | log_neg | log2_neg | log10_neg |
+--------------------+-----------+---------+----------+-----------+
| 0.6931471805599453 | 2.0       | NaN     | NaN      | NaN       |
+--------------------+-----------+---------+----------+-----------+
1 row(s) fetched.
Elapsed 0.004 seconds.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need a new file for the ln we can extend the already available macro for this.

@akmhatey-ai

Copy link
Copy Markdown
Author

Updated in 48152f0.

Covered the other negative-input cases from your example:

  • log now rejects negative values for float and decimal inputs.
  • log2 and log10 now use checked unary handling for negative Float32/Float64 inputs.
  • Invalid bases are unchanged and still follow the existing NaN behavior.

Validation run:

  • cargo test -p datafusion-functions test_log -- --nocapture
  • cargo test -p datafusion-sqllogictest --test sqllogictests -- scalar:640
  • cargo test -p datafusion-sqllogictest --test sqllogictests -- scalar:662
  • cargo test -p datafusion-sqllogictest --test sqllogictests -- scalar:711
  • cargo test -p datafusion-sqllogictest --test sqllogictests -- scalar:722
  • cargo test -p datafusion-sqllogictest --test sqllogictests -- scalar:750
  • cargo test -p datafusion-sqllogictest --test sqllogictests -- scalar:761
  • rustfmt --check datafusion/functions/src/math/log.rs datafusion/functions/src/macros.rs datafusion/functions/src/math/mod.rs
  • git diff --check
  • gitleaks detect --no-git on the changed files

One limitation: full cargo test -p datafusion-sqllogictest --test sqllogictests -- scalar still fails later on existing aggregate_test_100 expectations around lines 1551-1600, outside this log change.

@github-actions

github-actions Bot commented Jun 4, 2026

Copy link
Copy Markdown

Thank you for opening this pull request!

Reviewer note: cargo-semver-checks reported the current version number is not SemVer-compatible with the changes in this pull request (compared against the base branch).

Details
     Cloning apache/main
    Building datafusion-functions v53.1.0 (current)
       Built [  29.094s] (current)
     Parsing datafusion-functions v53.1.0 (current)
      Parsed [   0.084s] (current)
    Building datafusion-functions v53.1.0 (baseline)
       Built [  28.265s] (baseline)
     Parsing datafusion-functions v53.1.0 (baseline)
      Parsed [   0.084s] (baseline)
    Checking datafusion-functions v53.1.0 -> v53.1.0 (no change; assume patch)
     Checked [   0.534s] 222 checks: 222 pass, 30 skip
     Summary no semver update required
    Finished [  61.251s] datafusion-functions
    Building datafusion-sqllogictest v53.1.0 (current)
       Built [ 166.616s] (current)
     Parsing datafusion-sqllogictest v53.1.0 (current)
      Parsed [   0.024s] (current)
    Building datafusion-sqllogictest v53.1.0 (baseline)
       Built [ 168.783s] (baseline)
     Parsing datafusion-sqllogictest v53.1.0 (baseline)
      Parsed [   0.027s] (baseline)
    Checking datafusion-sqllogictest v53.1.0 -> v53.1.0 (no change; assume patch)
     Checked [   0.110s] 222 checks: 218 pass, 4 fail, 0 warn, 30 skip

--- failure feature_missing: package feature removed or renamed ---

Description:
A feature has been removed from this package's Cargo.toml. This will break downstream crates which enable that feature.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#cargo-feature-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.47.0/src/lints/feature_missing.ron

Failed in:
  feature memory-accounting in the package's Cargo.toml

--- failure function_missing: pub fn removed or renamed ---

Description:
A publicly-visible function cannot be imported by its prior path. A `pub use` may have been removed, or the function itself may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.47.0/src/lints/function_missing.ron

Failed in:
  function datafusion_sqllogictest::local_balance, previously in file /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/176a0136ab69b45357b9137038b732d8e332146a/datafusion/sqllogictest/src/accounting.rs:196
  function datafusion_sqllogictest::set_default_budget, previously in file /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/176a0136ab69b45357b9137038b732d8e332146a/datafusion/sqllogictest/src/accounting.rs:147
  function datafusion_sqllogictest::account_balance, previously in file /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/176a0136ab69b45357b9137038b732d8e332146a/datafusion/sqllogictest/src/accounting.rs:189
  function datafusion_sqllogictest::set_account_balance, previously in file /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/176a0136ab69b45357b9137038b732d8e332146a/datafusion/sqllogictest/src/accounting.rs:168
  function datafusion_sqllogictest::current_context_id, previously in file /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/176a0136ab69b45357b9137038b732d8e332146a/datafusion/sqllogictest/src/accounting.rs:127
  function datafusion_sqllogictest::memory_tracker_limit, previously in file /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/176a0136ab69b45357b9137038b732d8e332146a/datafusion/sqllogictest/src/accounting.rs:184
  function datafusion_sqllogictest::default_budget, previously in file /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/176a0136ab69b45357b9137038b732d8e332146a/datafusion/sqllogictest/src/accounting.rs:153
  function datafusion_sqllogictest::next_context_id, previously in file /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/176a0136ab69b45357b9137038b732d8e332146a/datafusion/sqllogictest/src/accounting.rs:99
  function datafusion_sqllogictest::settle_thread_local, previously in file /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/176a0136ab69b45357b9137038b732d8e332146a/datafusion/sqllogictest/src/accounting.rs:202
  function datafusion_sqllogictest::set_thread_context_id, previously in file /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/176a0136ab69b45357b9137038b732d8e332146a/datafusion/sqllogictest/src/accounting.rs:105
  function datafusion_sqllogictest::set_memory_tracker_limit, previously in file /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/176a0136ab69b45357b9137038b732d8e332146a/datafusion/sqllogictest/src/accounting.rs:179
  function datafusion_sqllogictest::reset_account_to_default, previously in file /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/176a0136ab69b45357b9137038b732d8e332146a/datafusion/sqllogictest/src/accounting.rs:162

--- failure pub_module_level_const_missing: pub module-level const is missing ---

Description:
A public const is missing or renamed
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.47.0/src/lints/pub_module_level_const_missing.ron

Failed in:
  SLT_TARGET_PARTITIONS in file /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/176a0136ab69b45357b9137038b732d8e332146a/datafusion/sqllogictest/src/test_context.rs:74

--- failure struct_missing: pub struct removed or renamed ---

Description:
A publicly-visible struct cannot be imported by its prior path. A `pub use` may have been removed, or the struct itself may have been renamed or removed entirely.
        ref: https://doc.rust-lang.org/cargo/reference/semver.html#item-remove
       impl: https://github.com/obi1kenobi/cargo-semver-checks/tree/v0.47.0/src/lints/struct_missing.ron

Failed in:
  struct datafusion_sqllogictest::OverdraftPanic, previously in file /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/176a0136ab69b45357b9137038b732d8e332146a/datafusion/sqllogictest/src/accounting.rs:140
  struct datafusion_sqllogictest::AccountingAllocator, previously in file /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/176a0136ab69b45357b9137038b732d8e332146a/datafusion/sqllogictest/src/accounting.rs:260
  struct datafusion_sqllogictest::AccountingMemoryPool, previously in file /home/runner/work/datafusion/datafusion/target/semver-checks/git-apache_main/176a0136ab69b45357b9137038b732d8e332146a/datafusion/sqllogictest/src/accounting_pool.rs:45

     Summary semver requires new major version: 4 major and 0 minor checks failed
    Finished [ 339.463s] datafusion-sqllogictest

@github-actions github-actions Bot added the auto detected api change Auto detected API change label Jun 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto detected api change Auto detected API change functions Changes to functions implementation sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostgreSQL compatibility: ln(-1.0::float8) should error, not return NaN

2 participants